package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;

import play.db.jpa.Model;

@Entity
public class EntityRelation extends Model {

	public long firstEntity;
	public long secondEntity;
	public String relationName;
	public String description;

	/**
	 * @author AbdelRahman Badawy
	 * 
	 *         This method saves in the database a new EntityRelation. It checks
	 *         also that it has not been saved before.
	 * @param FirstEntity
	 *            long id of the first entity
	 * @param SecondEntity
	 *            long id of the second Entity
	 * @param RelationName
	 *            String the name of the relation
	 * @param description
	 *            String description of the relation
	 * @return returns a String whether the relation is done or it is already
	 *         done
	 */
	public static String MakeEntityRelation(Long FirstEntity,
			Long SecondEntity, String RelationName, String description) {

		if (EntityRelation.find(
				"firstEntity=? and secondEntity=? and relationName=?",
				FirstEntity, SecondEntity, RelationName).first() == null) {

			EntityRelation Ent_Relation = new EntityRelation();
			Ent_Relation.firstEntity = FirstEntity;
			Ent_Relation.secondEntity = SecondEntity;
			Ent_Relation.relationName = RelationName;
			Ent_Relation.description = description;
			Ent_Relation.save();
			return "Relation is done !";

		} else {

			return "Relation is already done !";

		}

	}

	/**
	 * @author AbdelRahman Badawy This methods deletes EntityRelation from the
	 *         database
	 * @param relationId
	 *            long id of the relation
	 * @return String to mention whether relation is deleted or not
	 */
	public static String DelteRelationsEntity(Long relationId) {
		if (EntityRelation.findById(relationId) != null) {

			EntityRelation relationToBeDeleted = EntityRelation
					.findById(relationId);
			relationToBeDeleted.delete();
			return "Deleted!";
		} else {
			return "There is no Relation to Delete!";
		}
	}

	/**
	 * Returns a list of Strings that correspond to the entities' names that are
	 * related to a certain entity
	 * 
	 * @author shereef
	 * 
	 * @param Long
	 *            entityId Id of the entity
	 * @return list<String> entities return entities Name that are related to
	 *         that entity
	 */
	public static List<String> relatedEntity(Long entityId) {
		List<Long> entitiesIds = new ArrayList<Long>();
		List<String> entities = new ArrayList<String>();
		List<EntityRelation> entitiesRel = new ArrayList<EntityRelation>();
		if (EntityRelation
				.find("select tr from EntityRelation tr where tr.firstEntity =? or tr.secondEntity=? ",
						entityId) != null) {
			entitiesRel = EntityRelation
					.find("select tr from EntityRelation tr where tr.firstEntity =? or tr.secondEntity=? ",
							entityId).fetch();
			for (EntityRelation entityRel : entitiesRel) {
				if (entityRel.firstEntity == entityId
						&& !entitiesIds.contains(entityRel.secondEntity)) {
					Entities entity = (Entities) IdeaDeveloper
							.find("id=?", entityRel.secondEntity).fetch()
							.get(0);
					entitiesIds.add(entityRel.secondEntity);
					entities.add(entity.name);
				}
				if (entityRel.secondEntity == entityId
						&& !entitiesIds.contains(entityRel.firstEntity)) {
					Entities entity = (Entities) IdeaDeveloper
							.find("id=?", entityRel.firstEntity).fetch().get(0);
					entitiesIds.add(entityRel.firstEntity);
					entities.add(entity.name);
				}
			}
		}

		return entities;
	}

	/**
	 * @author AbedelRahman Badawy this methods returns the name of the relation
	 */
	public String toString() {
		return relationName;
	}
}
